package com.czh.auth.application.controller;

import com.alibaba.fastjson2.JSON;
import com.czh.auth.application.convert.AuthPermissionConverter;
import com.czh.auth.application.dto.AuthPermissionDTO;
import com.czh.auth.api.entity.ResponseResult;
import com.czh.auth.domain.entity.AuthPermissionBO;
import com.czh.auth.domain.service.AuthPermissionDomainService;
import com.google.common.base.Preconditions;
import io.micrometer.common.util.StringUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author caozaihui
 * @version 1.0`
 * @since 2024/8/30 下午 8:31
 */
@RestController
@RequestMapping("/permission/")
@Slf4j
public class PermissionController {

    @Resource
    private AuthPermissionDomainService authPermissionDomainService;

    /**
     * 查询用户权限
     * @param userName 用户名
     * @return List
     */
    @GetMapping("getPermission")
    public ResponseResult<List<String>> getPermission(@RequestParam("userName") String userName) {
        try {
            Preconditions.checkNotNull(userName,"用户名不能为空!");
            return ResponseResult.success(authPermissionDomainService.getPermission(userName));
        } catch (Exception e) {
            log.error("UserController:getPermission:error{}",e.getMessage(),e);
            return ResponseResult.error("查询用户权限失败");
        }
    }

    @PostMapping("add")
    public ResponseResult<Boolean> addPermission(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.add.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authPermissionDTO.getName()), "权限名称不能为空!");
            Preconditions.checkArgument(!StringUtils.isBlank(authPermissionDTO.getPermissionKey()), "权限标识不能为空!");
            Preconditions.checkNotNull(authPermissionDTO.getPermissionKey(), "权限类型不能为空!");
            Preconditions.checkNotNull(authPermissionDTO.getParentId(),"权限父id不能为空!");
            AuthPermissionBO authPermissionBO = AuthPermissionConverter.INSTANCE.convert(authPermissionDTO);
            return ResponseResult.success(authPermissionDomainService.addPermission(authPermissionBO));
        } catch (Exception e) {
            log.error("PermissionController.add.error:{}", e.getMessage(), e);
            return ResponseResult.error("创建权限失败!");
        }
    }
    @PostMapping("update")
    public ResponseResult<Boolean> updatePermission(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.updatePermission.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(),"权限id不能为空!");
            AuthPermissionBO authPermissionBO = AuthPermissionConverter.INSTANCE.convert(authPermissionDTO);
            return ResponseResult.success(authPermissionDomainService.updatePermission(authPermissionBO));
        } catch (Exception e) {
            log.error("PermissionController.update.error:{}", e.getMessage(), e);
            return ResponseResult.error("修改权限失败!");
        }
    }
    @PostMapping("delete")
    public ResponseResult<Boolean> deletePermission(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.deletePermission.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限id不能为空!");
            return ResponseResult.success(authPermissionDomainService.deletePermission(authPermissionDTO.getId()));
        } catch (Exception e) {
            log.error("PermissionController.delete.error:{}", e.getMessage(), e);
            return ResponseResult.error("删除权限失败!");
        }
    }
}
